package com.csdj.game.controller;

import com.csdj.game.constant.ResultCodeContant;
import com.csdj.game.constant.ViewNameConstant;
import com.csdj.game.entity.User;
import com.csdj.game.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * author:cloud
 * date:2021/3/30 0030
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }


    @PostMapping("/login")
    public ModelAndView doLogin(@RequestParam(value = "username",required = true)String username,
                                @RequestParam(value = "password",required = true)String password,
                                HttpServletRequest request){
        //校验参数
        if(password.length()<ResultCodeContant.MIN_PASSWROD_LENGTH){
            ModelAndView modelAndView = new ModelAndView("error");
            modelAndView.addObject("errMsg","密码不正确");
            return modelAndView;
        }
        boolean login = userService.login(request,username, password);
        if(login){
            //登录成功
            ModelAndView index = new ModelAndView("index");
            return index;
        }else{
            ModelAndView modelAndView = new ModelAndView("error");
            modelAndView.addObject("errMsg","密码不正确");
            return modelAndView;
        }
    }

    /**
     * 跳转到注册页面
     * @return
     */
    @GetMapping("/reg")
    public String reg(){
        return ViewNameConstant.REGIST;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/reg")
    public ModelAndView regist(User user){
        ModelAndView view = new ModelAndView();
        //数据校验
        if(user == null){
            view.setViewName(ViewNameConstant.ERROR);
            view.addObject(ResultCodeContant.ERROR_VIEW_MSG,
                    ResultCodeContant.errMsgMap.get(ResultCodeContant.NO_REG_DATA));
            return view;
        }
        if(user.getPassword().length()<ResultCodeContant.MIN_PASSWROD_LENGTH){
            view.setViewName(ViewNameConstant.ERROR);
            view.addObject(ResultCodeContant.ERROR_VIEW_MSG,
                    ResultCodeContant.errMsgMap.get(ResultCodeContant.PASSWORD_FORMAT_ERROR));
            return view;
        }
        int res = userService.regist(user);
        if(res == ResultCodeContant.SUCCESS){
            view.setViewName(ViewNameConstant.REG_SUCCESS);
            return view;
        }else{
            view.setViewName(ViewNameConstant.ERROR);
            view.addObject(ResultCodeContant.ERROR_VIEW_MSG,ResultCodeContant.errMsgMap.get(res));
            return view;
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return ViewNameConstant.INDEX;
    }

}
